package com.dmgis.qxfw.dataservice.businessservice.yyrb.service.Impl;

import com.dmgis.qxfw.dataservice.businessservice.api.domain.DbConfig;
import com.dmgis.qxfw.dataservice.businessservice.api.domain.RequestDataBySelect;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.base_product;
import com.dmgis.qxfw.dataservice.businessservice.common.util.DateTime;
import com.dmgis.qxfw.dataservice.businessservice.common.util.HttpRequestUtil;
import com.dmgis.qxfw.dataservice.businessservice.common.util.JsonUtil;
import com.dmgis.qxfw.dataservice.businessservice.common.util.StringUtil;
import com.dmgis.qxfw.dataservice.businessservice.controller.ProcessRequestController;
import com.dmgis.qxfw.dataservice.businessservice.yyrb.service.IFarmService;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("farmService")
public class FarmService implements IFarmService {
	ProcessRequestController process = new ProcessRequestController();
	HttpRequestUtil httUtil = new HttpRequestUtil();

	@Override
	public Date getLogNewDate() {
		// TODO 时间
		base_product newDate = new base_product();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		String[] columns = new String[] { "forecasttime" };
		selectRequest.setColumns(columns);
		StringBuilder strWhere = new StringBuilder();
		strWhere.append(String.format(" producttype='暴雨监测与评估产品'"));
		selectRequest.setParam(strWhere.toString());
		selectRequest.setSort(new String[] { "forecasttime desc" });
		selectRequest.setLimit(1);
		try {
			selectParam.put("method", "base_product");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = process.processRequest(selectParam);
			if(!message.equals("[]")){
				newDate = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<base_product>>() {
				}).get(0);
			}
		} catch (Exception e) {
			// TODO: handle exception
			newDate.setForecasttime(DateTime.now().toDate());
		}
		return newDate.getForecasttime();
	}

	@Override
	public List<base_product> getLogInfo(String startDate, String endDate, String type) {
		// TODO id
		List<base_product> listData = new ArrayList<base_product>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		StringBuilder strWhere = new StringBuilder();
		if (!StringUtil.IsNullOrEmpty(startDate)) {
			strWhere.append(String.format(" forecasttime>='%s'", startDate));
		}
		if (!StringUtil.IsNullOrEmpty(endDate)) {
			strWhere.append(String.format(" and forecasttime<='%s'", endDate));
		}
		strWhere.append(String.format(" and producttype='%s'", type));
		selectRequest.setParam(strWhere.toString());
		selectRequest.setSort(new String[]{"forecasttime desc"});
		try {
			selectParam.put("method", "base_product");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = process.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<base_product>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return listData;
	}

}
